package com.csun.dsms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.HgGdpDataMapper;
import com.csun.dsms.mapper.HgUserMapper;
import com.csun.dsms.model.huang.*;
import com.csun.dsms.service.HgGdpDataService;
import com.csun.dsms.service.HgUserService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HgGdpDataServiceImpl extends BaseService<HgGdpData> implements HgGdpDataService {

    @Resource
    private HgGdpDataMapper hgGdpDataMapper;

    @Override
    public Integer addHgGdpData(HgGdpData hgGdpData) {
        hgGdpData.setCreatedTime(new Date());
        return hgGdpDataMapper.insert(hgGdpData);
    }

    @Override
    public List<HgGdpData> queryGdpDataList(HgQueryDto hgQueryDto) {
        return hgGdpDataMapper.queryListWithPage(hgQueryDto);
    }

    @Override
    public Integer edit(HgGdpData hgGdpData) {
        return hgGdpDataMapper.updateByPrimaryKey(hgGdpData);
    }

    @Override
    public List<HgYearMonthVo> groupByMonth(HgQueryDto hgQueryDto) {
        return hgGdpDataMapper.groupByMonth(hgQueryDto);
    }

    @Override
    public Wrapper importGdpData(MultipartFile file) {
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> ExcelList = reader.readAll();
            if (CollectionUtil.isEmpty(ExcelList)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "Excel不能为空！");
            }
            for (Map<String, Object> excelMap : ExcelList) {
                HgGdpData HgGdpData = new HgGdpData();
                HgGdpData.setStatisticalYear(Convert.toInt(excelMap.get("统计年份")));
                HgGdpData.setStatisticalMonth(Convert.toInt(excelMap.get("统计月")));
                HgGdpData.setStatisticalQuarter(Convert.toInt(excelMap.get("统计季度")));
                HgGdpData.setStatisticalTimeType(Convert.toInt(excelMap.get("统计时间类型")));
                HgGdpData.setArea(Convert.toStr(excelMap.get("数据的区域")));
                HgGdpData.setAreaLevel(Convert.toInt(excelMap.get("是市的还是区的")));
                HgGdpData.setIndustry(Convert.toStr(excelMap.get("产业名称")));
                HgGdpData.setIndustryCategory(Convert.toStr(excelMap.get("行业名称")));
                HgGdpData.setIndustryLevel(Convert.toInt(excelMap.get("是三大产业数据还是九大行业数据还是其他, 0 是合在一起的")));
                HgGdpData.setGdp(Convert.toDouble(excelMap.get("gdp总量")));
                HgGdpData.setRanking(Convert.toInt(excelMap.get("gpd排名")));
                HgGdpData.setGrowthGdp(Convert.toDouble(excelMap.get("增长量")));
                HgGdpData.setGrowthRate(Convert.toDouble(excelMap.get("增速")));
                HgGdpData.setTopRate(Convert.toDouble(excelMap.get("上一级增速")));
                HgGdpData.setGrowthRateRanking(Convert.toInt(excelMap.get("增长率排名")));
                HgGdpData.setPerCapitaValue(Convert.toDouble(excelMap.get("人均gdp")));
                HgGdpData.setPerCapitaRanking(Convert.toInt(excelMap.get("人均gdp排名")));
                HgGdpData.setContributionRate(Convert.toDouble(excelMap.get("贡献率")));
                HgGdpData.setContributionRateRanking(Convert.toInt(excelMap.get("贡献率排名")));
                HgGdpData.setPullingRate(Convert.toDouble(excelMap.get("拉动率")));
                HgGdpData.setPullingRateRanking(Convert.toInt(excelMap.get("拉动率排名")));
                HgGdpData.setEntryDepartment(Convert.toStr(excelMap.get("上传的部门")));
                HgGdpData.setCreator(Convert.toStr(excelMap.get("上传人")));
                HgGdpData.setApproved(Convert.toInt(excelMap.get("是否审核")));
                HgGdpData.setApproval(Convert.toStr(excelMap.get("审核人")));
                HgGdpData.setCreatedTime(new Date());
                hgGdpDataMapper.insert(HgGdpData);
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, "导入GDP数据成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }    }
}
